package com.cqc.customdialog;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.Toast;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Calendar;

import es.dmoral.toasty.Toasty;

/**
 * Created by ${cqc} on 2017/4/19.
 */

public class Frag1 extends Fragment {

    private View rootView;
    private Calendar c;
    private Dialog datadialog = null;
    private String pickDate;
    private int today;
    protected String pickTime;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.frag1, container, false);
        Button btn = (Button) rootView.findViewById(R.id.btn);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showDataDialog();
            }
        });
        return rootView;
    }


    protected void showDataDialog() {
        c = Calendar.getInstance();
        datadialog = new MyDatePickerDialog(getActivity(), new MyDatePickerDialog.OnDateSetListener() {

            @Override
            public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                if (c.get(Calendar.YEAR) > year) {
                    Toast.makeText(getActivity(), "您选择的年份不对，请重新选择！", Toast.LENGTH_SHORT).show();
                    dialogDisplayOpear(false);// dialog显示控制-不关闭
                } else if (c.get(Calendar.YEAR) == year) {
                    if (c.get(Calendar.MONTH) > month) {
                        Toast.makeText(getActivity(), "您选择的月份不对，请重新选择！", Toast.LENGTH_SHORT).show();
                        dialogDisplayOpear(false);// dialog显示控制-不关闭
                    } else if (c.get(Calendar.MONTH) == month) {
                        if (c.get(Calendar.DAY_OF_MONTH) > dayOfMonth) {
                            Toast.makeText(getActivity(), "您选择的日期不对，请重新选择！", Toast.LENGTH_SHORT).show();
                            dialogDisplayOpear(false);// dialog显示控制-不关闭
                        } else {
                            Log.e("0011", "当前时间" + c.get(Calendar.HOUR_OF_DAY));
                            Log.e("0011", "当前日期" + c.get(Calendar.DAY_OF_MONTH));
                            Log.e("0011", "当前dayOfMonth" + dayOfMonth);
//                            if (c.get(Calendar.HOUR_OF_DAY) >= 22 && c.get(Calendar.DAY_OF_MONTH) == dayOfMonth) {
//                                Toast.makeText(getActivity(), "当日已无可预约时间，请选择其他日期", Toast.LENGTH_SHORT).show();
//                                dialogDisplayOpear(false);
//                            } else {
                                pickDate = String.valueOf(year) + "-" + String.valueOf(month + 1) + "-" + String.valueOf(dayOfMonth);
                                today = dayOfMonth;
                                dialogDisplayOpear(true);// dialog显示控制-关闭
                                showTimeDialog();
//                            }

                        }
                    } else {
                        pickDate = String.valueOf(year) + "-" + String.valueOf(month + 1) + "-" + String.valueOf(dayOfMonth);
                        today = dayOfMonth;
                        dialogDisplayOpear(true);// dialog显示控制-关闭
                        showTimeDialog();
                    }
                } else {
                    pickDate = String.valueOf(year) + "-" + String.valueOf(month + 1) + "-" + String.valueOf(dayOfMonth);
                    today = dayOfMonth;
                    dialogDisplayOpear(true);// dialog显示控制-关闭
                    showTimeDialog();
                }
            }

            private void dialogDisplayOpear(boolean isDisplay) {
                try {
                    Field field = datadialog.getClass().getSuperclass().getSuperclass().getSuperclass()
                            .getDeclaredField("mShowing");
                    field.setAccessible(true);
                    // 将mShowing变量设为false，表示对话框已关闭
                    field.set(datadialog, isDisplay);
                    datadialog.dismiss();
                } catch (Exception e) {
                }
            }
        }, c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH));
        datadialog.show();
    }

    protected void showTimeDialog() {
        final String[] item = filtrateTime(today);
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setIcon(android.R.drawable.ic_dialog_info);
        builder.setTitle("请选择预计到达时间");
        builder.setCancelable(false);
        final ChoiceOnClickListener choiceListener = new ChoiceOnClickListener();
        builder.setSingleChoiceItems(item, 0, choiceListener);
        DialogInterface.OnClickListener btnListener = new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialogInterface, int which) {
                int choiceWhich = choiceListener.getWhich();
                String hobbyStr = item[choiceWhich];
                pickTime = hobbyStr;
//                upData(pickDate, pickTime);
                dialogInterface.dismiss();
                Toasty.success(getActivity(),pickDate+","+pickTime).show();
            }
        };
        builder.setPositiveButton("确定", btnListener);
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface dialog, int which) {
                showDataDialog();
            }

        });
        builder.create().show();
    }

    private class ChoiceOnClickListener implements DialogInterface.OnClickListener {
        private int which = 0;

        public void onClick(DialogInterface dialogInterface, int which) {
            this.which = which;
        }

        public int getWhich() {
            return which;
        }
    }

    /**
     *
     * 方法描述：筛选时间 模块作者：Shawn-xiaolai 开发时间：2016年5月18日 下午12:45:33 更新记录：
     * 返回数据：String[]
     */
    private String[] filtrateTime(int today) {
        final String[] items = new String[] { "0:00-2:00", "2:00-4:00", "4:00-6:00", "6:00-8:00", "8:00-10:00",
                "10:00-12:00", "12:00-14:00", "14:00-16:00", "16:00-18:00", "18:00-20:00", "20:00-22:00", "22:00-24:00" };
        if (c.get(Calendar.DAY_OF_MONTH) == today) {
            ArrayList<String> list = new ArrayList<String>();
            for (int i = 0; i < items.length; i++) {
                if (c.get(Calendar.HOUR_OF_DAY) < Integer.parseInt(items[i].split("-")[0].replace(":00", ""))) {
                    list.add(items[i]);
                }
            }
            String[] newTime = (String[]) list.toArray(new String[list.size()]);
            return newTime;
        } else
            return items;

    }

}
